import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { ElMessage  } from 'element-plus'
import Login from '../views/Login/Login.vue'
import LeiZheng from './modules/LeiZheng/Routes'
import LuoChuan from './modules/LuoChuan/Routes'
import SunXuDong from './modules/SunXuDong/Routes'
import XueKai from './modules/XueKai/Routes'
import YeGuoPing from './modules/YeGuoPing/Routes'
import ZhuZhiHao from './modules/ZhuZhiHao/Routes'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    redirect:"/home/worktabe",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/HomeView.vue'),
    children:[
      ...LeiZheng,
      ...LuoChuan,
      ...SunXuDong,
      ...XueKai,
      ...YeGuoPing,
      ...ZhuZhiHao
    ]
  },
  
 
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path == '/') return next()
  const userInfo = JSON.parse(localStorage.getItem('userInfo')!) ? JSON.parse(localStorage.getItem('userInfo')!).password : ''
  if(JSON.parse(localStorage.getItem('userInfo')!) && userInfo){ next()}
  else{
    ElMessage({
      message: '请登录……',
      type: 'error',
    })
    next('/')
  }
})

export default router
